package com.xmz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmz.entity.Order;
import com.xmz.mapper.OrderMapper;
import com.xmz.service.IOrderService;
import com.xmz.util.PageResult;
import com.xmz.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public PageResult<Order> loadList(Integer pageNo, Integer pageSize, OrderVo orderVo){
        PageResult<Order> pageResult = new PageResult<>();
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if(orderVo != null){
            queryWrapper.like(StrUtil.isNotBlank(orderVo.getName()),"name",orderVo.getName())
                    .eq(orderVo.getState() != null,"state",orderVo.getState())
                    .eq(orderVo.getUserId()!= null && StrUtil.isNotBlank(orderVo.getUserId().toString()),
                            "user_id",orderVo.getUserId());
        }
        IPage<Order> page = new Page<>(pageNo,pageSize);
        IPage<Order> result = orderMapper.selectPage(page,queryWrapper);
        List<Order> records = result.getRecords();
        pageResult.setRows(records);
        pageResult.setTotal(result.getTotal());
        return  pageResult;
    }

    @Override
    public List<Order> getAllOrder(){
        return  orderMapper.selectList(null);
    }

    @Override
    public int addOrder(Order order) {
        return orderMapper.insert(order);
    }

    @Override
    public int deleteUser(List<Integer> id) {
        return orderMapper.deleteBatchIds(id);
    }

    @Override
    public int updateUser(Order order) {
        return orderMapper.updateById(order);
    }
}
